* for each facility, count occupants on first of each month
foreach x in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec {
foreach y in 2006 2007 2008 2009 2010 2011{
gen `x'`y'=0
replace `x'`y'=1 if rec_in<=td(01`x'`y') & rec_out>td(01`x'`y')
bysort labelclean: egen `x'`y'occ=total(`x'`y')
drop `x'`y'
}
}

* for each facility, check to see if they are full
foreach x in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec {
foreach y in 2006 2007 2008 2009 2010 2011{
gen `x'`y'full=0
replace `x'`y'full=1 if `x'`y'occ>0
}
}

*add all the occupancy variables together
gen tots=0
foreach x in Jan2006occ Jan2007occ Jan2008occ Jan2009occ Jan2010occ Jan2011occ Feb2006occ Feb2007occ Feb2008occ Feb2009occ Feb2010occ Feb2011occ Mar2006occ Mar2007occ Mar2008occ Mar2009occ Mar2010occ Mar2011occ Apr2006occ Apr2007occ Apr2008occ Apr2009occ Apr2010occ Apr2011occ May2006occ May2007occ May2008occ May2009occ May2010occ May2011occ Jun2006occ Jun2007occ Jun2008occ Jun2009occ Jun2010occ Jun2011occ Jul2006occ Jul2007occ Jul2008occ Jul2009occ Jul2010occ Jul2011occ Aug2006occ Aug2007occ Aug2008occ Aug2009occ Aug2010occ Aug2011occ Sep2006occ Sep2007occ Sep2008occ Sep2009occ Sep2010occ Sep2011occ Oct2006occ Oct2007occ Oct2008occ Oct2009occ Oct2010occ Oct2011occ Nov2006occ Nov2007occ Nov2008occ Nov2009occ Nov2010occ Nov2011occ Dec2006occ Dec2007occ Dec2008occ Dec2009occ Dec2010occ Dec2011occ{
replace tots=tots+`x'
}

* find out total number of full months
gen occfull=0
foreach x in Jan2006full Jan2007full Jan2008full Jan2009full Jan2010full Jan2011full Feb2006full Feb2007full Feb2008full Feb2009full Feb2010full Feb2011full Mar2006full Mar2007full Mar2008full Mar2009full Mar2010full Mar2011full Apr2006full Apr2007full Apr2008full Apr2009full Apr2010full Apr2011full May2006full May2007full May2008full May2009full May2010full May2011full Jun2006full Jun2007full Jun2008full Jun2009full Jun2010full Jun2011full Jul2006full Jul2007full Jul2008full Jul2009full Jul2010full Jul2011full Aug2006full Aug2007full Aug2008full Aug2009full Aug2010full Aug2011full Sep2006full Sep2007full Sep2008full Sep2009full Sep2010full Sep2011full Oct2006full Oct2007full Oct2008full Oct2009full Oct2010full Oct2011full Nov2006full Nov2007full Nov2008full Nov2009full Nov2010full Nov2011full Dec2006full Dec2007full Dec2008full Dec2009full Dec2010full Dec2011full{
replace occfull=occfull+`x'
}

* find average occupancy
gen occ= tots/occfull
replace occ=round(occ)
la var occ "Average occupancy"

*get rid of occ full
foreach x in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec {
foreach y in 2006 2007 2008 2009 2010 2011{
drop `x'`y'full
}
}

* drop all but July and January occupancies
foreach x in Feb Mar Apr May Jun Aug Sep Oct Nov Dec {
foreach y in 2006 2007 2008 2009 2010 2011{
drop `x'`y'occ
}
}

drop tots occfull
